package org.example.controller;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiOperation;
import org.example.domain.PoArriveorderVo;
import org.example.util.PdfUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/pdf")
public class PDFController {

    @ApiOperation(value = "打印PDF文件", produces = "application/pdf")
    @GetMapping("/print")
    public void printPDF(HttpServletResponse response){
        List<PoArriveorderVo> poArriveorderVoList = new ArrayList<>();
        // 创建第一个 PoArriveorderVo 对象
        PoArriveorderVo vo1 = new PoArriveorderVo();
        vo1.setId("ZSYS15052025040300001");
        vo1.setWeighingNo(" GB3WN4250331151437MR");
        vo1.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        vo1.setPackageBuckleHeavy(new BigDecimal("500.00"));
        vo1.setSrcOrderNo("NC001");
        vo1.setMaterialCode("M001");
        vo1.setMaterialName("钢材");
        vo1.setMaterialInfo("xxxx aaaa");
        vo1.setInventNum(new BigDecimal("100"));
        vo1.setLength(new BigDecimal("2.0"));
        vo1.setWidth(new BigDecimal("1.0"));
        vo1.setHeight(new BigDecimal("0.5"));
        vo1.setConversionStandard(new BigDecimal("1.0"));
        vo1.setArriveNum(new BigDecimal("200.00"));
        vo1.setTheoreticalAcceptanceWeight(new BigDecimal("480.00"));
        vo1.setOrgName("收货公司A");
        vo1.setPkOrgV("C001");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        vo1.setUpdateDate(null);
        vo1.setVbillcode("V001");
        vo1.setDmakedate(null);
        poArriveorderVoList.add(vo1);

        // 创建第二个 PoArriveorderVo 对象
        PoArriveorderVo vo2 = new PoArriveorderVo();
        vo2.setId("ZSYS15052025040300001");
        vo2.setWeighingNo("ZSYS150520250403009955");
        vo2.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        vo2.setPackageBuckleHeavy(new BigDecimal("600.00"));
        vo2.setSrcOrderNo("NC002");
        vo2.setMaterialCode("M002");
        vo2.setMaterialName("木材");
        vo2.setInventNum(new BigDecimal("200"));
        vo2.setLength(new BigDecimal("3.0"));
        vo2.setWidth(new BigDecimal("1.5"));
        vo2.setHeight(new BigDecimal("0.8"));
        vo2.setConversionStandard(new BigDecimal("1.2"));
        vo2.setArriveNum(new BigDecimal("300.00"));
        vo2.setTheoreticalAcceptanceWeight(new BigDecimal("598.24"));
        vo2.setOrgName("收货公司B");
        vo2.setPkOrgV("C002");
        vo2.setUpdateDate(new Date());
        vo2.setVbillcode("V002");
        vo2.setDmakedate(new Date());
        poArriveorderVoList.add(vo2);
        String theoreticalTotal = poArriveorderVoList.stream().map(PoArriveorderVo::getTheoreticalAcceptanceWeight).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add).toPlainString();
        String splitWeightTotal = poArriveorderVoList.stream().map(PoArriveorderVo::getArriveNum).filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, RoundingMode.HALF_UP).toPlainString();
        cn.hutool.json.JSONObject jsonObject = new JSONObject();
        jsonObject.set("poArriveorderVoList", poArriveorderVoList);
        jsonObject.set("theoreticalTotal",  theoreticalTotal);
        jsonObject.set("splitWeightTotal",  splitWeightTotal);
        PdfUtils.change1111(response,jsonObject.toString());
    }

    @PostMapping(value = "/param")
    public void printParam(@RequestParam Map<String, Object> map){
        System.out.println(JSON.toJSONString(map));
        Map<String, Object> map1 = JSON.parseObject(JSON.toJSONString(map), Map.class);
        System.out.println(map1);
    }
}
